package com.yhos.platform.oopston.core.validator;

import com.yhos.platform.oopston.core.annotation.NumberValid;
import com.yhos.platform.oopston.core.exception.BusinessException;
import org.apache.commons.lang3.math.NumberUtils;

public class NumberValidator extends ConstraintValidator<NumberValid, Object> {

    private String code;

    @Override
    public void initialize(NumberValid constraintAnnotation) {
        code = constraintAnnotation.message();
    }

    @Override
    public boolean isValid(Object value) {
        if (value == null) {
            throw new BusinessException(messageSourceUtils.getMessage(code), code);
        }
        if (value instanceof String) {
            if (NumberUtils.isCreatable(value.toString())) {
                throw new BusinessException(messageSourceUtils.getMessage(code), code);
            }
        }
        return true;
    }

}
